<template>
  <a-layout id="components-layout-demo-top" class="layout" style="min-height: 100%">
    <div id="header">
      <div class="logo">知&nbsp;否</div>

      <div id="title">

        <router-link :to="{name:'mainPage',params:{'id':this.id,'img':this.img,'token':this.token}}" style="color:white">
          <a-icon type="home" />&nbsp;首页
        </router-link>
      </div>

      <div id="title1">
        <router-link :to="{name:'personSpace',params:{'id':this.id,'img':this.img,'token':this.token}}" style="color:white">
          <a-icon type="user" />&nbsp;个人中心
        </router-link>
      </div>



      <a-input-search placeholder="请输入要搜索的问题" v-model="searchItem" style="width: 300px;margin: 15px 20px 10px 90px;" @search="onSearch"/>
      <button type="button" id="search"  style="
    height: 15px;
    width: 120px;
    margin: 10px;
    padding: 18px;
    border-radius: 10px;
    line-height: 0px;
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 20px;
    position: absolute;
    background: deepskyblue;
    font-weight: bold;
    color: white;
    border-color: skyblue;
    /*transform: translateY(4px);*/
">
        <router-link :to="{name:'addQuestion',params:{'id':this.id,'img':this.img,'token':this.token}}" style="color:white">
          提问题
        </router-link>

      </button>

      <div style="height:60px;weight:200px;float:right;padding: 0 20px;">


        <div id="right" style="padding: 15px 5px 20px 20px;">
          <a-badge :count=count title="通知" v-if=ifToken style="margin-left: 40px">
            <a-icon type="bell" style="font-size: 25px" title="通知" @click="getMessage"/>
          </a-badge>
<!--          <router-link :to="{name:'personSpace',params:{'id':this.id,'img':this.img,'token':this.token}}">
            <a-avatar shape="square" v-if=ifToken size="large" :src=img style="margin-left: 20px;margin-bottom: 12px"/>
          </router-link>-->
        </div>

        <template>
          <a-dropdown>
            <a style="color:white;font-size: 20px;" >
              {{ u_name }} <a-icon type="down" style="color:white;font-size:25px" />
            </a>
            <a-menu slot="overlay" @click="onClick">
              <a-menu-item key="1">
                <a-icon type="setting" />
                设置
              </a-menu-item>
              <a-menu-item key="2" @click="logout">
                <a-icon type="logout" />
                退出
              </a-menu-item>

            </a-menu>
          </a-dropdown>
        </template>

      </div>
    </div>





    <a-layout-content style="padding: 0 50px">
      <a-breadcrumb style="margin: 20px 0">
      </a-breadcrumb>
      <div :style="{ background: '#fff', padding: '24px', minHeight: '430px' }">
<!--        问题部分-->
        <a-page-header
            style=""
            sub-title="返回"
            @back="exit"
        />
        <h2 style="font-weight: bold">{{question_title}}</h2>

        <p style="font-size: 15px">{{question_content}}</p>

        <p style="font-size: 10px">{{question_updated_time}}</p>

        <a-button v-if="question_state === 0" @click="collect" style="width:90px;padding: 0 10px;"><a-icon type="star" />收藏&nbsp;{{this.question_collection_num}}</a-button>

        <a-button type="primary" v-if="question_state === 0" @click="write" style="width:90px;padding: 0 10px;"><a-icon type="edit" />写回答</a-button>

        <a-divider />
<!--        问题部分-->


<!--        回答区-->
        <div v-for="item in answerList" :key="item.answer_id">
<!--          <img :src=item.u_img width="42" height="42">&nbsp;&nbsp;-->
        <p style="font-weight: bold;font-size: 15px;display: inline">{{item.u_name}}</p>
          <br/><br/>
          {{item.answer_content}}
          <br/><br/>
          <p style="font-size: 10px">{{item.updated_time}}</p>
          <a-button type="primary" @click="like(item)" style="width:60px;padding: 0 10px;"><a-icon type="like" />{{item.praise_num}}</a-button>&nbsp;
          <a-button type="primary" @click="dislike(item)" style="width:60px;padding: 0 10px;"><a-icon type="dislike" />{{item.criticize_num}}</a-button>
          <a-button type="primary" style="margin-left: 100px;width:70px;padding: 0 10px;" @click="info(item)"><a-icon type="message" theme="twoTone" />评论</a-button>
          <a-divider/>
        </div>
<!--        回答区-->

        </div>
    </a-layout-content>

  </a-layout>
</template>
<script>
export default {
  name:"detailPage",
  inject:['reload'],
  data(){
    return{
      question_title:this.$route.params.question_title,//问题标题
      question_content:this.$route.params.question_content,//问题内容
      question_id: this.$route.params.question_id,//路由传值
      question_state:this.$route.params.question_state,//问题状态
      question_updated_time:this.$route.params.question_updated_time,//问题更新时间
      question_collection_num:'',//收藏问题的人数
      answerList:[],
      answerId:'',
      replyList:[],
      a_id:3,
      reply:'',//回复内容
      searchItem:'',//搜索的内容
      id:this.$route.params.id,//从登陆界面进行传值
      img:this.$route.params.img,
      token:this.$route.params.token,
      count:'',
      ifToken:localStorage.getItem('access_token'),
      u_name:'',
      question_user_id: this.$route.params.question_user_id


    }
  },
  mounted() {
    var _this = this;
    console.log("传过来的question_title:"+this.$route.params.question_title)
    console.log("从mainPage页面传过来的question_updated_time:"+this.$route.params.question_updated_time)


    _this.$axios.post('/api/user/selectByUserId',
        _this.$qs.stringify({
          uId:_this.id,
        }),
        {headers:{access_token:_this.token}}
    ).then(function (response) {
      console.log("主页获取登录用户信息：");
      console.log(response.data);
      console.log(response.data.data.u_img);
      console.log(response.data.data.u_name);
      _this.img = response.data.data.u_img;
      _this.u_name=response.data.data.u_name;
    }).catch(function (error) {
      console.log(error);
      //_this.$message.error('请求失败');
    })

    _this.$axios.post('/api/answer/showAnswerByTime',
        _this.$qs.stringify({
          questionId:_this.question_id,
        }),{headers:{access_token:_this.token}}
    ).then(function (response) {
      console.log("showAnswerByTime请求成功");
      console.log(response.data);
      console.log(response.data.data)
      if(response.data.code === -1)
        _this.$message.info('该问题还没有回答');
      else
        _this.answerList = response.data.data;
    }).catch(function (error) {
      console.log(error);
      alert("请求失败");
    })

    _this.$axios.post('/api/msg/showMsgNum',
        _this.$qs.stringify({
          userId: _this.$route.params.id,//router传值！！！
        }),{headers:{access_token:_this.token}}
    ).then(function (response) {
      console.log(response.data.data);
      _this.count = response.data.data;
      //_this.list = response.data.data;
    }).catch(function (error) {
      console.log(error);
      _this.$message.error('获取消息失败');
    })
  },//end getQuestion函数，得到问题的详细信息

  methods:{
    collect(){
      var _this=this;
      console.log("所收藏的问题id为："+this.question_id)
      console.log("所收藏的问题的提出者为："+this.question_user_id)
      console.log("已收藏该问题的人数有："+this.$route.params.question_collection_num)
      _this.question_collection_num=this.$route.params.question_collection_num
      _this.$axios.post('/api/quest/alterQuestCollectionState',
          _this.$qs.stringify({
            questionId:_this.question_id,
            uId:_this.question_user_id, //提出该问题的人
            operatorId:_this.id,
            isDeleted:3
          }),{headers:{access_token:_this.token}}
      ).then(function(response){
        console.log("response.data.data:");
        console.log(response.data)
        if(response.data.code === 0){
          _this.$message.success('收藏成功');

          _this.question_collection_num+=1
          console.log("目前的收藏人数有："+_this.question_collection_num)
        }
        else
          _this.$message.info(response.data);

        _this.reload();
      }).catch(function(error){
        console.log(error);
        _this.$message.error('请求失败');
      })
    },


    logout(){
      var _this = this;
      _this.$axios.post('/api/user/leave',
          _this.$qs.stringify({
            uId: this.id,
          }),{headers: {access_token:_this.token}}
      ).then(function (response) {
        console.log(response.data);
        localStorage.removeItem('access_token');
        localStorage.clear();
        _this.$router.push({
          path:'/login',
          name:'login',
        })
      }).catch(function (error) {
        console.log(error);
        _this.$message.error('请求失败');
      })
    },//end exit函数/*
    onClick ({ key }) {
      console.log(`Click on item ${key}`)
    },

    exit(){
      var _this = this;
      _this.$router.push({
        path:'/mainPage',
        name:'mainPage',
        params:{
          id:_this.id,
          img:_this.img,
          token:_this.token,
        }
      })
    },

    write(){
      var _this = this;
      _this.$router.push({
        path:'/writeAnswer',
        name:'writeAnswer',//路由命名
        params:{
          question_id:_this.question_id,//页面之间的传值
          id:_this.id,
          img:_this.img,
          token:_this.token,
          question_title:_this.question_title,
          question_content:_this.question_content,
          question_state:_this.question_state,
        },
      });
    },//end write函数，写回答

    like(record){
      var _this = this;
      //console.log(record);
      _this.$axios.post('/api/answer/alterAnswerEvaluationState',
        _this.$qs.stringify({
          answerId: record.answer_id,//回答编号
          uId: record.user_id,//用户账号
          operatorId: _this.id,//操作者账号？？？？？需要进行修改
          evaluationState: 4,//回答被赞
        }),{ headers: { access_token: _this.token } }
      ).then(function (response) {
          console.log(response);
          if(response.data.code === 90)
            _this.$message.error('不能进行重复操作');
          else if(response.data.code === 90000)
            _this.$message.error('请先登录再对该回答进行操作');
          else
            _this.$message.success('点赞成功');
          console.log("点赞数："+record.praise_num)
        _this.reload();
        }).catch(function (error) {
          console.log(error);
          _this.$message.error('请求失败');
        })//end catch异常
    },//end like函数，赞回答

    dislike(record){
      var _this = this;
      _this.$axios.post('/api/answer/alterAnswerEvaluationState',
          _this.$qs.stringify({
            answerId: record.answer_id,//回答编号
            uId: record.user_id,//用户账号
            operatorId: _this.id,//操作者账号？？？？？需要进行修改
            evaluationState: 5,//回答被赞
          }),{ headers: { access_token: _this.token } }
      ).then(function (response) {
            console.log(response.data);
            if(response.data.code === 90)
              _this.$message.error('不能进行重复操作');
            else if(response.data.code === 90000)
              _this.$message.error('请先登录再对该回答进行操作');
            else
              _this.$message.success('点踩成功');
            _this.reload();
          }).catch(function (error) {
            console.log(error);
            _this.$message.error('请求失败');
          })//end catch异常
    },//end dislike函数。踩回答

    showReplyByTime(answer_id){
      var _this = this;
      _this.$axios.post( '/api/showReplyByTime',
          _this.$qs.stringify({
            answer_id: answer_id,
          })
      ).then(function (response) {
        console.log(response.data);
        _this.replyList = response.data.data();
      }).catch(function (error) {
        console.log(error);
        _this.$notification.error({
          message: '请求失败',
          // description: '',
        });
      })
    },//end showReplyByTime函数，按照时间顺序展示回复

    info(record) {
      var _this = this;
      _this.$router.push({
        path: '/getComments',
        name: 'getComments',//路由命名
        params: {
          question_title:_this.question_title,
          answer_content:record.answer_content,
          answer_id:record.answer_id,
          id:_this.id,
          img:_this.img,
          token:_this.token,
          question_id:_this.question_id,
          question_state:_this.question_state,
          question_content: _this.question_content,
        },
      })
    },//end info函数，提供弹窗功能

    addReply(formName){
      var _this = this;
      _this.$axios.post('/api/addAnswer',
          _this.$qs.stringify({
            answerId: _this.answerId,
            userId:2,//使用router
            replyContent:_this.reply,
          })
      ).then(function (response) {
        console.log("请求成功");
        console.log(response.data);
        _this.$message.success('成功提交评论');
        _this.$router.push({
          path:'/detailpage',
          name:'DetailPage',//路由命名
          params:{
            id:_this.id,
          },
        });
      }).catch(function (error){
        console.log(error);
        console.log(formName.text);
        _this.$message.error('请求失败，请重新提交评论');
      })
    },//end addReply函数,添加回复

    onSearch(){
      var _this = this;
      _this.$axios.post('/api/quest/selectByTitle',
          _this.$qs.stringify({
            questionTitle:_this.searchItem,
          }),{ headers: { access_token: _this.token } }
      ).then(function (response) {
        console.log(response.data.data);
        _this.$router.push({
          path:'/searchPage',
          name:'searchPage',//路由命名
          params:{
            list:response.data.data,
            id:_this.id,
            img:_this.img,
            token:_this.token
          },
        });//end 页面跳转
      }).catch(function (error) {
        console.log(error);
        _this.$message.error('请求失败');
      })
    },//end onSearch函数，进行查找问题

    getMessage(){
      var _this = this;
      _this.$router.push({
        path:'/messagePage',
        name:'messagePage',
        params:{
          id:_this.id,
          img:_this.img,
          token:_this.token,
        }
      })
    },//end getMessage函数，获取消息
  }//end methods
}
</script>
<style lang="less" scoped>

</style>
